package com.yiren.dbaa.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.view.adapter.SingleLightAdapter;
import com.yiren.dbaa.view.dragsortlistview.DragSortListView;

import java.util.List;

/**
 * 类描述： 灯光组合选择Dialog
 * 创建人： lwb
 * 创建时间：2021年11月5日 8:33:50
 */
public class GroupLightSelectDialog extends Dialog {

    public static DragSortListView mlistview; //列表内容
    public static SingleLightAdapter mDialogSingle;

    public GroupLightSelectDialog(Context context) {
        super(context);
    }

    public GroupLightSelectDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private Activity activity;
        private String title;
        private boolean isCanceled = true;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private List<SingleLightEntity> mlist;
        private int ChoiceType;


        public Builder(Context context, List<SingleLightEntity> mlist, int choiceType) {
            this.context = context;
            this.mlist = mlist;
            this.ChoiceType = choiceType;
        }

        public Builder(Activity activity, Context context, List<SingleLightEntity> mlist, int choiceType) {
            this.context = context;
            this.activity = activity;
            this.mlist = mlist;
            this.ChoiceType = choiceType;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Set the Dialog title from String
         * 是否点击空白处消失
         *
         * @return
         */

        public Builder setCanceled(boolean isCanceled) {
            this.isCanceled = isCanceled;
            return this;
        }


        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         * 左边按钮
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        /**
         * 两边按钮
         *
         * @param listener
         * @return
         */
        public Builder setNegativeButton(int negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public GroupLightSelectDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final GroupLightSelectDialog dialog = new GroupLightSelectDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_grouplight_select, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            DisplayMetrics metric = new DisplayMetrics();
            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
            params.width = (metric.widthPixels * 8) / 10;
            params.height = (metric.heightPixels * 7) / 10;
            dialog.setCanceledOnTouchOutside(isCanceled);// 设置点击屏幕Dialog不消失
            dialog.getWindow().setAttributes(params);
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            mlistview = (DragSortListView) layout.findViewById(R.id.dragsortlistview_listview);
            if (ChoiceType == 4) {
                mDialogSingle = new SingleLightAdapter(activity, context, mlist, ChoiceType);
            } else {
                mDialogSingle = new SingleLightAdapter(context, mlist, ChoiceType);
            }

            mlistview.setAdapter(mDialogSingle);
            dialog.setOnKeyListener(new OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (isCanceled) {
                        dialog.dismiss();
                        return false;
                    } else {
                        return true;
                    }
                }
            });


            // set the confirm button  
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.btn_add)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.btn_add)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.btn_add).setVisibility(View.GONE);
            }

            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.btn_close)).setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.btn_close)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE  
                layout.findViewById(R.id.btn_close).setVisibility(View.GONE);
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}  